package org.openea.eap.module.obpm.util.form.tableexcel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.openbpm.bus.api.constant.BusColumnCtrlType;
import org.openbpm.bus.api.model.IBusinessColumn;
import org.openbpm.sys.api.model.dto.DataDictDTO;
import org.openbpm.sys.api.service.DataDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;













@Service
public class DicExcellor
  extends BusinessColumnExcellor
{
  @Autowired
  private DataDictService dataDictService;

  BusColumnCtrlType getCtrlType() { return BusColumnCtrlType.DIC; }



  Object fromExcel(IBusinessColumn column, Object text, JSONObject config) {
    String[] ts = text.toString().split(",");
    List<String> ls = new ArrayList<>();

    List<DataDictDTO> dictNodeList = this.dataDictService.getDictNodeList(config.getString("key"), Boolean.valueOf(false));
    for (String t : ts) {
      boolean exist = false;
      for (DataDictDTO dto : dictNodeList) {
        if (dto.getName().equals(t)) {
          exist = true;
          ls.add(dto.getKey());
        }
      }
      if (!exist) {
        ls.add(t);
      }
    }

    return StrUtil.join(",", ls.toArray());
  }


  Object toExcel(IBusinessColumn column, Object value, JSONObject config) {
    String[] vs = value.toString().split(",");
    List<String> ls = new ArrayList<>();

    List<DataDictDTO> dictNodeList = this.dataDictService.getDictNodeList(config.getString("key"), Boolean.valueOf(false));
    for (String v : vs) {
      boolean exist = false;
      for (DataDictDTO dto : dictNodeList) {
        if (dto.getKey().equals(v)) {
          exist = true;
          ls.add(dto.getName());
        }
      }
      if (!exist) {
        ls.add(v);
      }
    }

    return StrUtil.join(",", ls.toArray());
  }
}
